package com.organization;

/**
 * Created with IntelliJ IDEA.
 * User: inester
 * Date: 05.07.12
 * Time: 15:22
 * To change this template use File | Settings | File Templates.
 */

import org.jgroups.View;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.jgroups.JChannel;
import org.jgroups.Message;
import org.jgroups.ReceiverAdapter;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;
import java.util.HashMap;
import java.util.Map;

public class ServerApp extends ReceiverAdapter
{
    private JChannel channel;
    private final String user_name="com.organization.Server";
    private static ServerApp instance = null;
    //MBean server for register our mbeans
    private MBeanServer mbs = null;
    //Application setting
    private Settings settings = null;

    public static ServerApp getInstance(){
        if (instance == null){
            instance = new ServerApp();
        }
        return instance;
    }

    //constructor
    public ServerApp(){
        //create inctanse of Settings
        settings = new Settings();
    }

    public Settings getSettings(){
        return settings;
    }

    public void sendMessage(String nodeId,String action){
        try {
            Map<String,String> mess = new HashMap<String, String>();
            mess.put("node",nodeId);
            mess.put("action",action);
            mess.put("source",user_name);
            Message msg=new Message(null, null, mess);
            channel.send(msg);
        }
        catch(Exception e) {
        }
    }

    public void viewAccepted(View new_view) {
        System.out.println("** view: " + new_view);
    }

    public void receive(Message msg) {
        String line=msg.getSrc() + ": " + msg.getObject();
        System.out.println(line);
    }

    private void start() throws Exception {
        channel=new JChannel();
        channel.setReceiver(this);
        channel.connect("com.organization.Cluster");
        eventLoop();
        channel.close();
    }

    private void eventLoop() {
        while(true) {
        }
    }

    private void registerManagedBeans()
    {
        mbs = ManagementFactory.getPlatformMBeanServer();
        ObjectName settingsBeanName = null;
        try {
            settingsBeanName = new ObjectName("OrganizationServerParameters:name=serverParams");
            mbs.registerMBean(settings, settingsBeanName);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main( String[] args ) throws Exception
    {
        //register server settings for change it through jmx
        ServerApp.getInstance().registerManagedBeans();
        //start rmi server
        new ClassPathXmlApplicationContext("config/BeanLocations.xml");
        //start JChannel connection
        ServerApp.getInstance().start();
    }
}